/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.agreement.service.serviceinvoice;

import java.util.List;

import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.agreement.entity.serviceinvoice.TserviceInvoice;
import com.jeesite.modules.agreement.dao.serviceinvoice.TserviceInvoiceDao;
import com.jeesite.modules.agreement.entity.serviceinvoice.TserviceInvoiceDetail;
import com.jeesite.modules.agreement.dao.serviceinvoice.TserviceInvoiceDetailDao;

/**
 * 服务发票Service
 * @author shengbin
 * @version 2021-03-04
 */
@Service
@Transactional(readOnly=true)
public class TserviceInvoiceService extends CrudService<TserviceInvoiceDao, TserviceInvoice> {
	
	@Autowired
	private TserviceInvoiceDetailDao tserviceInvoiceDetailDao;
	@Autowired
	private TserviceInvoiceDao tserviceInvoiceDao;


	public List<TserviceInvoice>findListByQuery(TserviceInvoice tserviceInvoice){
		return tserviceInvoiceDao.findListByQuery(tserviceInvoice);
	}
	/**
	 * 获取单条数据
	 * @param tserviceInvoice
	 * @return
	 */
	@Override
	public TserviceInvoice get(TserviceInvoice tserviceInvoice) {
		TserviceInvoice entity = super.get(tserviceInvoice);
		if (entity != null){
			TserviceInvoiceDetail tserviceInvoiceDetail = new TserviceInvoiceDetail(entity);
			tserviceInvoiceDetail.setStatus(TserviceInvoiceDetail.STATUS_NORMAL);
			entity.setTserviceInvoiceDetailList(tserviceInvoiceDetailDao.findList(tserviceInvoiceDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tserviceInvoice 查询条件
	 * @param tserviceInvoice.page 分页对象
	 * @return
	 */
	@Override
	public Page<TserviceInvoice> findPage(TserviceInvoice tserviceInvoice) {
		return super.findPage(tserviceInvoice);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tserviceInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TserviceInvoice tserviceInvoice) {
		super.save(tserviceInvoice);
		// 保存 TserviceInvoice子表
		for (TserviceInvoiceDetail tserviceInvoiceDetail : tserviceInvoice.getTserviceInvoiceDetailList()){
			if (!TserviceInvoiceDetail.STATUS_DELETE.equals(tserviceInvoiceDetail.getStatus())){
				tserviceInvoiceDetail.setServiceInvoiceId(tserviceInvoice);
				if (tserviceInvoiceDetail.getIsNewRecord()){
					tserviceInvoiceDetail.setCreateBy(UserUtils.getUser().getUserCode());
					tserviceInvoiceDetail.setUpdateBy(UserUtils.getUser().getUserCode());
					tserviceInvoiceDetailDao.insert(tserviceInvoiceDetail);
				}else{
					tserviceInvoiceDetail.setUpdateBy(UserUtils.getUser().getUserCode());
					tserviceInvoiceDetailDao.update(tserviceInvoiceDetail);
				}
			}else{
				tserviceInvoiceDetailDao.delete(tserviceInvoiceDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tserviceInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TserviceInvoice tserviceInvoice) {
		super.updateStatus(tserviceInvoice);
	}
	
	/**
	 * 删除数据
	 * @param tserviceInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TserviceInvoice tserviceInvoice) {
		super.delete(tserviceInvoice);
		TserviceInvoiceDetail tserviceInvoiceDetail = new TserviceInvoiceDetail();
		tserviceInvoiceDetail.setServiceInvoiceId(tserviceInvoice);
		tserviceInvoiceDetailDao.deleteByEntity(tserviceInvoiceDetail);
	}
	
}